// const videoDb = require('../db/video')
const { logger } = require("../logs/index");
const { playLine } = require("../config/index");
const videoModel = require("../model/video");
const analytical = require("../model/analytical");
const { queryTvList, queryVideoByUrl, createVideo } = require("../model/tvDb");

exports.getVideoUrl = async (ctx) => {
  let param = ctx.request.body;
  let url = param.url;
  let title = param.title;
  let cid = param.cid;
  let source = param.source;

  if (!url) {
    ctx.body = {
      code: 500,
      message: "url不能为空",
    };
  }

  if (!source) {
    ctx.body = {
      code: 500,
      message: "source不能为空！",
    };
  }
  try {
    //如果数据库存在
    let playUrl = await analytical(url);
    if (playUrl) {
      ctx.body = {
        code: 200,
        message: "请求成功",
        data: playUrl,
      };
    } else {
      ctx.body = {
        code: 500,
        message: "视频解析失败，请重试！",
        data: null,
      };
    }
  } catch (error) {
    logger.error(error);
    ctx.body = {
      code: 500,
      message: "系统错误！",
    };
  }
};

exports.getTvList = async (ctx) => {
  let res = await queryTvList();
  if (res && Array.isArray(res)) {
    ctx.body = {
      code: 200,
      data: res,
      message: "请求成功",
    };
  } else {
    ctx.body = {
      code: 500,
      data: null,
      message: "请求失败",
    };
  }
};

exports.getHomeData = async (ctx) => {
  try {
    let param = ctx.request.body;
    let data = await videoModel.getHomeData(param);
    if (data.code) {
      ctx.body = {
        code: 200,
        data: data.data,
        message: "请求成功",
      };
    } else {
      ctx.body = {
        code: 500,
        data: "",
        message: "系统错误",
      };
    }
  } catch (error) {
    logger.error(error);
    ctx.body = {
      code: 500,
      data: "",
      message: "系统错误",
    };
  }
};

exports.getFilterList = async (ctx) => {
  try {
    let param = ctx.request.body;
    let data = await videoModel.getFilterList(param);
    if (data.code) {
      ctx.body = {
        code: 200,
        data: data.data,
        message: "请求成功",
      };
    } else {
      ctx.body = {
        code: 500,
        data: "",
        message: "系统错误",
      };
    }
  } catch (error) {
    logger.error(error);
    ctx.body = {
      code: 500,
      data: "",
      message: "系统错误",
    };
  }
};

exports.getFilterVideo = async (ctx) => {
  try {
    let param = ctx.request.body;
    let data = await videoModel.getFilterVideo(param);
    if (data.code) {
      ctx.body = {
        code: 200,
        data: data.data,
        message: "请求成功",
      };
    } else {
      ctx.body = {
        code: 500,
        data: "",
        message: "系统错误",
      };
    }
  } catch (error) {
    logger.error(error);
    ctx.body = {
      code: 500,
      data: "",
      message: "系统错误",
    };
  }
};

exports.getVideoDesc = async (ctx) => {
  try {
    let param = ctx.request.body;
    let data = await videoModel.getVideoDesc(param);
    if (data.code) {
      ctx.body = {
        code: 200,
        data: data.data,
        message: "请求成功",
      };
    } else {
      ctx.body = {
        code: 500,
        data: "",
        message: "系统错误",
      };
    }
  } catch (error) {
    logger.error(error);
    ctx.body = {
      code: 500,
      data: "",
      message: "系统错误",
    };
  }
};

exports.getVideoList = async (ctx) => {
  try {
    let param = ctx.request.body;
    let data = await videoModel.getVideoList(param);
    if (data.code) {
      ctx.body = {
        code: 200,
        data: data.data,
        message: "请求成功",
      };
    } else {
      ctx.body = {
        code: 500,
        data: "",
        message: "系统错误",
      };
    }
  } catch (error) {
    logger.error(error);
    ctx.body = {
      code: 500,
      data: "",
      message: "系统错误",
    };
  }
};

exports.getVideoWord = async (ctx) => {
  try {
    let param = ctx.request.body;
    let data = await videoModel.getVideoWord(param);
    if (data.code) {
      ctx.body = {
        code: 200,
        data: data.data,
        message: "请求成功",
      };
    } else {
      ctx.body = {
        code: 500,
        data: "",
        message: "系统错误01",
      };
    }
  } catch (error) {
    logger.error(error);
    ctx.body = {
      code: 500,
      data: "",
      message: "系统错误02",
    };
  }
};

exports.getVideoSearchList = async (ctx) => {
  try {
    let param = ctx.request.body;
    let data = await videoModel.getVideoSearchList(param);
    if (data.code) {
      ctx.body = {
        code: 200,
        data: data.data,
        message: "请求成功",
      };
    } else {
      ctx.body = {
        code: 500,
        data: "",
        message: "系统错误",
      };
    }
  } catch (error) {
    logger.error(error);
    ctx.body = {
      code: 500,
      data: "",
      message: "系统错误",
    };
  }
};
